package com.inossem.warehousestorage_inossem.adapter.allocation;

import android.content.Context;
import android.view.LayoutInflater;
import android.view.View;
import android.view.ViewGroup;
import android.widget.BaseAdapter;
import android.widget.TextView;

import com.inossem.warehousestorage_inossem.R;
import com.inossem.warehousestorage_inossem.bean.allocation.AllocationInMatsBean;
import com.inossem.warehousestorage_inossem.utils.AllocationUtils;
import com.zhy.android.percent.support.PercentRelativeLayout;

import java.util.List;

/**
 * TODO
 *
 * @author 詹建宇
 * @time on 2018/4/8 10:48
 * @email jianyu.zhan@inossem.com
 */
public class AllocationInBatchDetailsAdapter extends BaseAdapter {

    private Context context;
    private String condition;
    private AllocationInMatsBean.MatsBodyBean.ItemListBean itemListBean;
    private List<AllocationInMatsBean.MatsBodyBean.ItemListBean.StockPositionListBean> stockPositionList;//批次列表
    private String outNum;//出库数量
    private double inNum;//入库数量
    private String unit;

    public AllocationInBatchDetailsAdapter(Context context, String condition, AllocationInMatsBean.MatsBodyBean.ItemListBean itemListBean,
                                           List<AllocationInMatsBean.MatsBodyBean.ItemListBean.StockPositionListBean> stockPositionList) {
        this.context = context;
        this.condition = condition;
        this.itemListBean = itemListBean;
        this.stockPositionList = stockPositionList;
        this.outNum = itemListBean.getQty();
        this.inNum = AllocationUtils.getInQtyByMatId(context, itemListBean.getMat_id());
        this.unit = itemListBean.getUnit_code();
    }

    @Override
    public int getCount() {
        return stockPositionList.size() + 1;
    }

    @Override
    public Object getItem(int i) {
        return null;
    }

    @Override
    public long getItemId(int i) {
        return 0;
    }

    @Override
    public View getView(int position, View convertView, ViewGroup parent) {
        if (position == 0) {
            convertView = LayoutInflater.from(context).inflate(R.layout.cy_view_in_storage_allocation_mat_first, null);
            //信息
            TextView codeTv = (TextView) convertView.findViewById(R.id.codeTv);
            TextView describeTv = (TextView) convertView.findViewById(R.id.describeTv);
            TextView outNumTv = (TextView) convertView.findViewById(R.id.outNumTv);
            TextView inNumTv = (TextView) convertView.findViewById(R.id.inNumTv);

            codeTv.setText(itemListBean.getMat_code());
            describeTv.setText(itemListBean.getMat_name());
            outNumTv.setText(String.valueOf(outNum + unit));
            inNumTv.setText(String.valueOf(inNum + unit));

            PercentRelativeLayout layout = (PercentRelativeLayout) convertView.findViewById(R.id.searchLayout);
            layout.setVisibility(View.GONE);

        } else {
            ViewHolder holder;
            if (convertView == null) {
                convertView = LayoutInflater.from(context).inflate(R.layout.cy_view_in_storage_allocation_mat_batch_item, null);
                holder = new ViewHolder();
                holder.batch = (TextView) convertView.findViewById(R.id.batch);
                holder.supplierBatch = (TextView) convertView.findViewById(R.id.supplierBatch);
                holder.stockNumber = (TextView) convertView.findViewById(R.id.stockNumber);
                holder.outStorageText = (TextView) convertView.findViewById(R.id.outStorageText);
                holder.batchTextView = (TextView) convertView.findViewById(R.id.batchTextView);
                convertView.setTag(holder);
            } else {
                holder = (ViewHolder) convertView.getTag();
            }
            AllocationInMatsBean.MatsBodyBean.ItemListBean.StockPositionListBean bean = stockPositionList.get(position - 1);
            holder.batch.setText(String.valueOf(bean.getBatch()));
            holder.supplierBatch.setText(String.valueOf(bean.getSupplier_batch()));

            double outNum = Double.parseDouble(bean.getQty());
            double inNum = AllocationUtils.getInQtyByMatIdBatch(context, itemListBean.getMat_id(), bean.getBatch());

            holder.stockNumber.setText(String.valueOf("到货数量\t" + outNum + unit));
            holder.outStorageText.setText(String.valueOf("入库数量\t" + inNum + unit));

            if (outNum == inNum) {
                holder.batch.setBackgroundResource(R.color.base_green);
                holder.batchTextView.setBackgroundResource(R.color.base_green);
            } else {
                holder.batch.setBackgroundResource(R.color.view_receive_item_list_dark_row);
                holder.batchTextView.setBackgroundResource(R.color.view_receive_item_list_dark_row);
            }

        }
        return convertView;
    }

    private class ViewHolder {
        private TextView batch;
        private TextView supplierBatch;
        private TextView stockNumber;
        private TextView outStorageText;
        private TextView batchTextView;
    }

    public String getOutNum() {
        return outNum;
    }

    public String getInNum() {
        return String.valueOf(inNum);
    }

}
